home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 425_01 / lzpipe / doz.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-24  |  3.8 KB  |  150 lines

  1. /* doz.c: 'Do Z' test/demo program for UNIX-like compression */
  2.  
  3. #ifndef __TURBOC__
  4. #    include <sys/types.h>
  5. #else
  6. #    include <io.h>
  7. #endif
  8. #include <sys/stat.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11.  
  12. #include "lzpipe.h"
  13.  
  14. FILE *inp, *out;
  15. struct stat s;
  16. long this;
  17.  
  18. static int getbyte()
  19. {
  20.    register b;
  21.    b = getc(inp);
  22.    if (ferror(inp)) {
  23.       perror("Read error"); exit(-1);
  24.    }
  25.    return b;
  26. }
  27.  
  28. static int putbyte(int b)
  29. {
  30.    if (putc(b, out) == EOF) {
  31.       perror("Write error"); exit(-1);
  32.    }
  33.    return 0;
  34. }
  35.  
  36. static void prratio(num, den)
  37. long num, den;
  38. {
  39.    register long q;    /* permits |result| > 655.36% */
  40.  
  41.    if (num > 214748L) { /* 2147483647/10000 */
  42.       q = (int)(num / (den / 10000L));
  43.    } else {
  44.       q = (int)(10000L * num / den); /* Long calculations, though */
  45.    }
  46.    if (q < 0) {
  47.       fprintf(stderr, "-");
  48.       q = -q;
  49.    }
  50.    fprintf(stderr, "%d.%02d%%", (int)(q / 100), (int)(q % 100));
  51. }
  52.  
  53. static char todo = ' ';
  54. static int bits = 16;
  55.  
  56. main(argc, argv)
  57. char *argv[];
  58. {
  59.    char buf[1024];
  60.    register char *p;
  61.    register i;
  62.    long l;
  63.  
  64.    for (i=1; i<argc && *(p=argv[i]) == '-'; i++) {
  65.       while (*++p) {
  66.          switch (*p) {
  67.             case 'C': case 'c':
  68.                if (todo != ' ') goto ambiguos;
  69.                todo = 'c';
  70.                break;
  71.             case 'D': case 'd':
  72.                if (todo != ' ') goto ambiguos;
  73.                todo = 'd';
  74.                break;
  75.             ambiguos:
  76.                (void)fprintf(stderr, "The only \'c\' or \'d\' is allowed\n");
  77.                return -1;
  78.             case 'B': case 'b':
  79.                bits = atoi(argv[++i]);
  80.                if (bits < 9 || bits > 16) {
  81.                   (void)fprintf(stderr, "Invalid bits factor\n");
  82.                   return -1;
  83.                }
  84.                break;
  85.             default :
  86.                (void)fprintf(stderr, "Unknown option \'%c\'\n", *p);
  87.                return -1;
  88.          }
  89.       }
  90.    }
  91.    if (i+2 != argc) {
  92.       (void)fprintf(stderr, "Usage: doz -c|d [-b nn] <input> <output>\n");
  93.       return -1;
  94.    }
  95.    if ((inp = fopen(argv[i], "rb")) == NULL) {
  96.       perror(argv[i]); exit(-1);
  97.    }
  98.    if ((out = fopen(argv[i+1], "wb")) == NULL) {
  99.       perror(argv[i+1]); exit(-1);
  100.    }
  101.    this = 0;
  102.    if (fstat(fileno(inp), &s) != 0) {
  103.       perror(argv[i]); return -1;
  104.    }
  105.    switch (todo) {
  106.       case 'c':
  107.          if ((i = lzwcreat(putbyte, s.st_size, bits)) < bits) {
  108.             if (i > 0) (void)fprintf(stderr, "Can only handle %d bits\n", i);
  109.             lzwfree();
  110.             return -1;
  111.          }
  112.          l = s.st_size;
  113.          while (l > 0) {
  114.             i = l > sizeof(buf) ? sizeof(buf) : (int)l;
  115.             if (read(fileno(inp), buf, i) != i) {
  116.                perror("Read error"); return -1;
  117.             }
  118.             (void)lzwwrite(buf, i);
  119.             l -= i;
  120.          }
  121.          l = lzwclose();
  122.          prratio(l, s.st_size);
  123.          i = l >= s.st_size;
  124.          break;
  125.       case 'd':
  126.          if ((i = lzwopen(getbyte)) != 0) {
  127.             if (i == -1) {
  128.                (void)fprintf(stderr, "File is not in compressed format\n");
  129.             } else {
  130.                (void)fprintf(stderr, "Not enough memory");
  131.                if (i >= 12) (void)fprintf(stderr," to process %d bits",i);
  132.                (void)fprintf(stderr, "\n");
  133.             }
  134.             return -1;
  135.          }
  136.          do {
  137.             i = lzwread(buf, sizeof(buf));
  138.             if (i > 0 && write(fileno(out), buf, i) != i) {
  139.                (void)fprintf(stderr, "Write error\n");
  140.                return -1;
  141.             }
  142.          } while (i == sizeof(buf));
  143.          break;
  144.       default :
  145.          (void)fprintf(stderr, "Either \'c\' or \'d\' must be specified\n");
  146.          return -1;
  147.    }
  148.    return i;
  149. }
  150.